/*
 * Copyright (c) 2018. 国脉海洋信息发展有限公司
 */

package com.gov.dna.base.controller;

import com.gov.dna.base.entity.BaseModel;
import com.gov.dna.base.mybatis.BasePage;
import com.gov.dna.base.mybatis.BaseQuery;
import com.gov.dna.base.service.BaseService;
import com.gov.dna.common.util.R;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import javax.validation.Valid;
import java.time.LocalDateTime;

/**
 * 通用Controller get 统一键值对的形式请求 post 统一JSON形式的请求
 * @author (作者) Dong Jie 154046519@qq.com
 * @version V1.0
 * @date 2018/12/12 11:20
 * @since (该版本支持的JDK版本) 1.8
 */
public class BaseController<M extends BaseService<T>,T extends BaseModel> {

	@Autowired
	protected M baseService;


	/**
	 * 通用返回分页查询结果
	 * @author (作者) Dong Jie 154046519@qq.com
	 * @param page
	 * @param o
	 * @return com.cloud.dips.common.core.base.mybatis.BasePage<T>
	 * @date 2018/12/12 14:11
	 */
	@GetMapping("/list")
	@ApiOperation(value = "通用返回分页查询结果", notes = "通用返回分页查询结果", httpMethod = "GET")
	public R list(BasePage<T> page, T o) {
		return new R(baseService.findByPage(o,page));
	}

	/**
	 * 通用返回所有查询结果不分页
	 * @author (作者) Dong Jie 154046519@qq.com
	 * @param query
	 * @param o
	 * @return com.cloud.dips.common.core.util.R
	 * @date 2018/12/12 14:11
	 */
	@GetMapping("/listAll")
	@ApiOperation(value = "通用返回所有查询结果不分页", notes = "通用返回所有查询结果不分页", httpMethod = "GET")
	public R listAll(BaseQuery<T> query, T o) {
		return new R(baseService.find(o,query));
	}

	/**
	 * 返回单个查询结果通用接口
	 * @author (作者) Dong Jie 154046519@qq.com
	 * @param o
	 * @return com.cloud.dips.common.core.util.R
	 * @date 2018/12/12 14:10
	 */
	@GetMapping("/findSingle")
	@ApiOperation(value = "通用返回单个查询结果通用接口", notes = "返回单个查询结果通用接口: params{ID: id}", httpMethod = "GET")
	public R findSingle(T o){
		return new R(baseService.findSingle(o));
	}

	/**
	 * 通用新增接口
	 * @author (作者) Dong Jie 154046519@qq.com
	 * @param o
	 * @return com.cloud.dips.common.core.util.R
	 * @date 2018/12/12 14:08
	 */
	@PostMapping("/insert")
	@ApiOperation(value = "通用新增接口", notes = "通用新增接口", httpMethod = "POST")
	public R insert(@Valid @RequestBody T o) {
		return new R(baseService.insert(o));
	}


	/**
	 * 通用修改全部字段接口
	 * @author (作者) Dong Jie 154046519@qq.com
	 * @param o
	 * @return com.cloud.dips.common.core.util.R
	 * @date 2018/12/17 9:18
	 */
	@PostMapping("/updateAllColumn")
	@ApiOperation(value = "通用修改全部字段接口", notes = "通用修改全部字段接口", httpMethod = "POST")
	public R updateAllColumn(@Valid @RequestBody T o) {
		o.setModifiedTime(LocalDateTime.now());
		return new R(baseService.updateAllColumnById(o));
	}

	/**
	 * 修改非空字段接口
	 * @author (作者) Dong Jie 154046519@qq.com
	 * @param o
	 * @return com.cloud.dips.common.core.util.R
	 * @date 2018/12/12 14:10
	 */
	@PostMapping("/update")
	@ApiOperation(value = "通用修改非空字段接口", notes = "修改非空字段接口", httpMethod = "POST")
	public R update(@Valid @RequestBody T o) {
		o.setModifiedTime(LocalDateTime.now());
		return new R(baseService.updateById(o));
	}

	/**
	 * 通用删除
	 * @author (作者) Dong Jie 154046519@qq.com
	 * @param o
	 * @return com.cloud.dips.common.core.util.R
	 * @date 2018/12/12 11:25
	 */
	@PostMapping("/delete")
	@ApiOperation(value = "通用删除接口", notes = "根据ID删除: params{类型ID: id}", httpMethod = "POST")
	public R delete(@Valid @RequestBody T o) {
		return new R(baseService.logicalDeleteById(o));
	}
}
